package com.javasm.mb_store_api_final.ordersys.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.javasm.common.R;
import com.javasm.common.RandomUtil;
import com.javasm.mb_store_api_final.giftsandgoods.goods.entity.GgGoods;
import com.javasm.mb_store_api_final.giftsandgoods.goods.service.GgGoodsService;
import com.javasm.mb_store_api_final.ordersys.order.entity.OrderTable;
import com.javasm.mb_store_api_final.ordersys.order.service.OrderTableService;


import com.javasm.mb_store_api_final.ordersys.util.ExportUtil;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xmm
 * <p>
 * &#064;@Description:
 * <p>
 * &#064;@Date 2024/7/31 下午9:35
 */
@RestController
@RequestMapping("/order")
public class OrdrtController {
    @Resource
    OrderTableService orderService;

    @Resource
    GgGoodsService GoodsService;

    private static void replacePlaceholders(XWPFParagraph paragraph, JsonNode jsonNode) {

        for (XWPFRun run : paragraph.getRuns()) {
            String text = run.getText(0);

            System.out.println(text);
            if (text != null) {
                // 替换订单基本信息的占位符
                text = text.replace("oid", jsonNode.get("oid").asText());
                text = text.replace("order_date", jsonNode.get("orderDate").asText());
                text = text.replace("otype", jsonNode.get("otype").asText());
                text = text.replace("oaction", jsonNode.get("otype").asText());
                if (jsonNode.get("oaction").asText().equals("0") ){
                    text = text.replace("褚","待处理");
                }else {
                    text = text.replace("褚","已处理");
                }
                text = text.replace("ostyle", jsonNode.get("ostyle").asText());
                text = text.replace("uname", jsonNode.get("uname").asText());
                text = text.replace("ucount", jsonNode.get("ucount").asText());
                text = text.replace("uphone", jsonNode.get("uphone").asText());
                text = text.replace("mail", jsonNode.get("mail").asText());
                text = text.replace("uzid", jsonNode.get("uzid").asText());
                text = text.replace("place", jsonNode.get("place").asText());
                text = text.replace("area", jsonNode.get("area").asText());
                text = text.replace("虱", jsonNode.get("way").asText());
                text = text.replace("fire", jsonNode.get("fire").asText());
                text = text.replace("payway", jsonNode.get("payway").asText());


                // 替换订单商品信息的占位符
                JsonNode goodsNode = jsonNode.get("goods").get(0);
                text = text.replace("暝", goodsNode.get("gname").asText());
                text = text.replace("price", goodsNode.get("marketPrice").asText());
                text = text.replace("嗳", goodsNode.get("gcode").asText());
                text = text.replace("莨", goodsNode.get("stock").asText());

                // 将修改后的文本设置回run
                run.setText(text, 0);
            }
        }
    }


    @PostMapping("/buxiangxiele")
    public List<Integer> addOrder() {
        Integer a =  orderService.gerNum();
        Integer b =  orderService.gerOrderNum();
        List<Integer> list = new ArrayList<>();
        list.add(a);
        list.add(b);
        return list;
    }


    @Resource
    ExportUtil<OrderTable> exportUtil;


    @PostMapping("/query")
    public R queryAll(@RequestBody OrderTable orderTable,Integer currentPage, Integer pageSize) throws IOException {

        List<OrderTable> Orders = orderService.queryBy(orderTable,currentPage, pageSize);

        List<OrderTable> Orderss = orderService.queryBy(orderTable,1, 100);

        Ordersss=Orderss;
        int size = Orderss.size();


        return R.okk(Orders,size);
    }
    List<OrderTable> Ordersss;


    @GetMapping("/query2")
    public void queryAll2() throws IOException {



        exportUtil.expExcel("动态列表",Ordersss, OrderTable.class);
    }
    @PostMapping("/insert")
    public void insert(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            throw new RuntimeException("上传的文件为空");
        }

        // 读取 Excel 文件
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());

        // 单独设置头信息，与实体类中的属性名保持一致
        reader.addHeaderAlias("学号", "id");
        reader.addHeaderAlias("姓名123", "name");

        List<OrderTable> list = reader.readAll(OrderTable.class);

        // 批量插入到数据库
        orderService.saveBatch(list);
    }

    @Autowired
    private ResourceLoader resourceLoader;

    @GetMapping("/query1")
    public ResponseEntity<?> queryAll1(@RequestParam String fileName, HttpServletResponse response) throws IOException {

        org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:static/dynamic.xlsx");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return ResponseEntity.ok().headers(headers).body(resource.getInputStream());

    }


//    @PostMapping("/detil")
//    public R detail(Integer oid){
//        OrderTable one = orderService.getById(oid);
//        List<GgGoods> ggs = GoodsService.getByOrder(oid);
//        double all = 0;
//        for (int i = 0; i < ggs.size(); i++) {
//            int randomInt = (int)(Math.random() * (5 - 2 + 1)) + 2;
//            ggs.get(i).setStock(randomInt);
//            all +=randomInt*ggs.get(i).getPurchasePrice();}
//        one.setGoods(ggs);
//        return R.ok(one,all);
//    }





    }



